package com.nsgf.library.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.WaterDetailInfoBean;
import com.nsgf.library.data.bean.WaterRecordsBean;
import com.nsgf.skjz.utils.UnitTo;

import java.util.ArrayList;
import java.util.List;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
@SuppressLint("ViewConstructor")
public class MyMarkerView extends MarkerView {

    private TextView tvDate;
    private TextView tvValue;
    private TextView tv_title;
    private View view_color;
    private ValueFormatter xAxisValueFormatter;
    private ValueFormatter yAxisValueFormatter;
    private String title = "", unit = "#3CC3DB";
    private List<WaterDetailInfoBean.WaterRecordsListBean> list = new ArrayList<>();

    public MyMarkerView(Context context,
                        ValueFormatter xAxisValueFormatter,
                        ValueFormatter yAxisValueFormatter,
                        String title,
                        String unit) {
        super(context, R.layout.custom_marker_view);
        this.xAxisValueFormatter = xAxisValueFormatter;
        this.yAxisValueFormatter = yAxisValueFormatter;

        tvDate = findViewById(R.id.tv_date);
        tvValue = findViewById(R.id.tv_value);
        view_color = findViewById(R.id.view_color);
        tv_title = findViewById(R.id.tv_title);
        this.title = title;
        this.unit = unit;
    }

    public MyMarkerView(Context context,
                        ValueFormatter xAxisValueFormatter,
                        ValueFormatter yAxisValueFormatter,
                        String title,
                        String unit,
                        List<WaterDetailInfoBean.WaterRecordsListBean> list) {
        super(context, R.layout.custom_marker_view);
        this.xAxisValueFormatter = xAxisValueFormatter;
        this.yAxisValueFormatter = yAxisValueFormatter;

        tvDate = findViewById(R.id.tv_date);
        tvValue = findViewById(R.id.tv_value);
        tv_title = findViewById(R.id.tv_title);
        view_color = findViewById(R.id.view_color);
        this.title = title;
        this.unit = unit;
        this.list = list;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        //展示自定义X轴值 后的X轴内容
        try {
            tvDate.setText(list.get((int) e.getX()).getTm());
        } catch (Exception ex) {
            ex.getMessage();
            tvDate.setText(xAxisValueFormatter.getAxisLabel(e.getX(), null));
        }
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.OVAL);
        drawable.setColor(Color.parseColor(unit));
        view_color.setBackground(drawable);
        tvValue.setText(String.valueOf(e.getY()));
        tv_title.setText(title);
        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        // 默认偏移量，会被 getOffsetForDrawingAtPoint 覆盖
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }

    @Override
    public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) {
        // 获取图表视图
        View chartView = getChartView();
        if (chartView == null) {
            return getOffset();
        }
        
        // 获取marker的宽度和高度
        float markerWidth = getWidth();
        float markerHeight = getHeight();
        
        // 获取图表边界
        float chartWidth = chartView.getWidth();
        float chartHeight = chartView.getHeight();
        
        // 额外的间距，避免遮挡数据点
        float spacing = Utils.convertDpToPixel(10);
        
        // 动态调整X轴偏移 - 水平居中，但如果超出边界则调整
        float xOffset = -(markerWidth / 2);
        float markerLeft = posX + xOffset;
        float markerRight = markerLeft + markerWidth;
        
        if (markerLeft < spacing) {
            // marker超出左边界，调整到右侧
            xOffset = spacing;
        } else if (markerRight > chartWidth - spacing) {
            // marker超出右边界，调整到左侧
            xOffset = -(markerWidth + spacing);
        }
        
        // 动态调整Y轴偏移 - 默认显示在数据点上方，不遮挡
        float yOffset = -(markerHeight + spacing);
        float markerTop = posY + yOffset;
        float markerBottom = posY + spacing; // 如果显示在下方时的位置
        
        // 如果上方空间不足，显示在下方
        if (markerTop < spacing) {
            yOffset = spacing;
        }
        // 如果下方空间也不足，强制显示在上方（可能会超出边界，但至少不遮挡数据点）
        else if (markerBottom > chartHeight - spacing) {
            yOffset = -(markerHeight + spacing);
        }
        
        return new MPPointF(xOffset, yOffset);
    }
}
